/**
 * 邮件参数信息维护
 * @author liuw
 * @date 2019/11/04
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        element,
        vueData = {

        },
        table_where = {

        },
        cols = [[
            {type: 'checkbox', title: '选择', fixed: 'left'},
            {field: 'ID', width: 110, title: '流水号',hide:true},
            {field: 'MAIL_SMTP', width: 140, title: '邮件服务器'},
            {field: 'MAIL_PORT', width: 100, title: '端口'},
            {field: 'MAIL_EMAIL',width: 100, title: '发送邮箱账号',search:"default" },
            {field: 'PASSWORD',width: 100, title: '发送邮箱密码'},
            {field: 'MAIL_PAW',width: 100, title: '发送邮箱密码',hide:true},
            {field: 'MAIL_TYPE',width: 100, title: '邮箱类型',filterDatas: app.datas_('system_mail_mailType'),
                search:"default",searchOpts:{type:'select'}
            },
            {field: 'MAILUD001',width:100,title:"是否启用",filterDatas: app.datas_('sys_n_y'),
                search:"default",searchOpts:{type:'select'}
            },
            {
                width: 60,
                title: '操作',
                toolbar: '#script-mailsetting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }

        ]],
        mailSettingAction,
        moduleName = '邮件参数',
        editArea = ['480px', '430px']
    ;

    var mail = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'mail',
                _searchs: [{
                    id: 'mail', tableId: 'mail_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'mail_edit', tpl: 'system/mail_edit'
                }]
            }, win);
        },
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'mail_table'),
                    url: g_ctxPath + 'sys/mail/getMailList',
                    exportsName: moduleName + '数据',
                    title: moduleName + '表',
                    method: 'post',
                    id: 'mail_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: false,
                    page: true,
                    where: table_where,
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-mailsetting-action').innerHTML).render({}, function (string) {
                    mailSettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(mail_table)', function (obj) {

                    layer.tips(mailSettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            tipsIndex = index;
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'mail_edit',
                                            tagData: {
                                                obj : obj,
                                                moduleName : moduleName
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: moduleName + '编辑',
                                                area: editArea
                                            }
                                        });
                                        break;
                                    case '1':
                                        app.confirm({
                                            msg: '是否删除选择的数据？',
                                            ok: function () {
                                                self.deleteData(obj);
                                            }
                                        });
                                        break;
                                }
                                layer.close(index);
                                tipsIndex = null;
                            });
                        }
                    });
                    return false;
                });

                //监听行双击事件
                table.on('rowDouble(mail_table)', function (obj) {
                    self._openTplFull({
                        tag: 'mail_edit',
                        tagData: {
                            obj : obj,
                            moduleName : moduleName
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '编辑',
                            area: editArea
                        }
                    });
                });

                //监听事件
                table.on('toolbar(mail_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'mail_edit',
                                tagData: {
                                    is_add : true,
                                    moduleName : moduleName
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '编辑',
                                    area: editArea
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.deleteDataBatch();
                                }
                            });
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },
        reloadTableCurr: function () {
            self._reloadTable({id: 'mail_table', where: table_where});
        },
        deleteData: function (obj) {
            if(obj.data.MAILUD001  == 'Y') {
                app.showWarning("已经启用的参数不可删除");
                return;
            }
            app.ajax({
                xurl: 'sys/mail/del',
                xdata: obj.data,
                loading: true,
                okmsg: moduleName +'信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });
        },
        deleteDataBatch: function(){
            var checkStatus = table.checkStatus('mail_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                var canDelete = true;
                checkStatus.data.forEach(function (v) {
                    if(v.MAILUD001 == 'Y' ) {
                        canDelete = false;
                        return;
                    }
                    var _temp = {ID:''};
                    _temp.ID = v.ID;
                    _xdata.push(_temp);
                });
                if(!canDelete) {
                    app.showWarning("存在已启用的邮件参数，无法批量删除");
                    return;
                }
                app.ajax({
                    xurl: 'sys/mail/delBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: moduleName +'信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },
        _ready: function () {
            vueData.serchData = table_where;
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    searchId: 'mail',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;

                        }
                    }
                }]
            });

            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new mail();

});